export default {
  name: 'Login',
  data () {
    return {
      info: {},
      loginRules: {
        username: [
          { required: true, message: '请填写用户名', trigger: 'change' },
          { min: 3, message: '最少输入三个字符', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请填写密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    resetHandler () {
      this.$refs.login.resetFields()
    },
    submitHandler () {
      this.$refs.login.validate(async valid => {
        if (!valid) {
          this.$message({
            message: '请按照规则完整填写表单',
            type: 'warning',
            duration: 1500,
            showClose: true
          })
          return
        }
        const { data, meta } = await this.$post('login', this.info)
        if (meta.status === 400) {
          this.$message({
            type: 'error',
            duration: 2000,
            message: '用户名或密码错误，请重新登录'
          })
          return
        }
        window.sessionStorage.setItem('token', data.token)

        this.$message({
          type: 'success',
          message: '登录成功, 即将跳转',
          duration: 1500
        })
        setTimeout(() => {
          this.$router.push('/home')
        }, 1500)
      })
    }
  }
}
